package com.example.restatr.Atcivity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.restatr.Atcivity.Main.MainActivity;
import com.example.restatr.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class SplashAt extends AppCompatActivity {
    private ViewPager splash;
    private View yuan1;
    private View yuan2;
    private View yuan3;
    private View yuan4;
    private View yuan5;
    private TextView NetSet;
    private TextView JoinHome;
    private int pos;
    private Timer timer = new Timer();
    private SharedPreferences sp;
    private Boolean isFirst;
    private List<ImageView> imageViews = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView( R.layout.activity_splash_at);

        splash = findViewById(R.id.splash);
        yuan1 = findViewById(R.id.yuan1);
        yuan2 = findViewById(R.id.yuan2);
        yuan3 = findViewById(R.id.yuan3);
        yuan4 = findViewById(R.id.yuan4);
        yuan5 = findViewById(R.id.yuan5);
        NetSet = findViewById(R.id.NetSet);
        JoinHome = findViewById(R.id.Commit);
        sp = getSharedPreferences("splash", MODE_PRIVATE);
        isFirst = sp.getBoolean("isFirst", true);
        for (int i = 0; i < 5; i++) {
            ImageView imageView = new ImageView(this);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageViews.add(imageView);
        }
        imageViews.get(0).setImageResource(R.drawable.qdy);
        imageViews.get(1).setImageResource(R.drawable.zhjt);
        imageViews.get(2).setImageResource(R.drawable.zhsq);
        imageViews.get(3).setImageResource(R.drawable.zhyl);
        imageViews.get(4).setImageResource(R.drawable.zhhb);

        if (isFirst) {
            splash.setAdapter(new spAdapter());
            splash.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {
                    switch (position) {
                        case 0:
                            yuan1.setBackgroundResource(R.drawable.select_yuan);
                            yuan2.setBackgroundResource(R.drawable.no_select_yuan);
                            yuan3.setBackgroundResource(R.drawable.no_select_yuan);
                            yuan4.setBackgroundResource(R.drawable.no_select_yuan);
                            yuan5.setBackgroundResource(R.drawable.no_select_yuan);
                            break;
                        case 1:
                            yuan1.setBackgroundResource(R.drawable.no_select_yuan);
                            yuan2.setBackgroundResource(R.drawable.select_yuan);
                            yuan3.setBackgroundResource(R.drawable.no_select_yuan);
                            yuan4.setBackgroundResource(R.drawable.no_select_yuan);
                            yuan5.setBackgroundResource(R.drawable.no_select_yuan);
                            break;
                        case 2:
                            yuan1.setBackgroundResource(R.drawable.no_select_yuan);
                            yuan2.setBackgroundResource(R.drawable.no_select_yuan);
                            yuan3.setBackgroundResource(R.drawable.select_yuan);
                            yuan4.setBackgroundResource(R.drawable.no_select_yuan);
                            yuan5.setBackgroundResource(R.drawable.no_select_yuan);
                            break;
                        case 3:
                            yuan1.setBackgroundResource(R.drawable.no_select_yuan);
                            yuan2.setBackgroundResource(R.drawable.no_select_yuan);
                            yuan3.setBackgroundResource(R.drawable.no_select_yuan);
                            yuan4.setBackgroundResource(R.drawable.select_yuan);
                            yuan5.setBackgroundResource(R.drawable.no_select_yuan);
                            break;
                        case 4:
                            yuan1.setBackgroundResource(R.drawable.no_select_yuan);
                            yuan2.setBackgroundResource(R.drawable.no_select_yuan);
                            yuan3.setBackgroundResource(R.drawable.no_select_yuan);
                            yuan4.setBackgroundResource(R.drawable.no_select_yuan);
                            yuan5.setBackgroundResource(R.drawable.select_yuan);
                            break;
                    }
                    if (position == 4) {
                        JoinHome.setVisibility(View.VISIBLE);
                        NetSet.setVisibility(View.VISIBLE);
                    } else {
                        JoinHome.setVisibility(View.GONE);
                        NetSet.setVisibility(View.GONE);
                    }
                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });
            timer.schedule(timerTask, 3000, 3000);
            sp.edit().putBoolean("isFirst", false).apply();
        } else {
            finish();
            startActivity(new Intent(this, MainActivity.class));
        }

    }

    TimerTask timerTask = new TimerTask() {
        @Override
        public void run() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    pos = splash.getCurrentItem();
                    pos++;
                    splash.setCurrentItem(pos);
                    if (pos == imageViews.size() - 1) {
                        timer.cancel();
                    }
                }
            });
        }
    };

    public void splashClick(View view) {
        switch (view.getId()) {
            case R.id.Commit:
                finish();
                startActivity(new Intent(this, MainActivity.class));
                break;
            case R.id.NetSet:
                View inflate = getLayoutInflater().inflate(R.layout.dailog_layout, null);
                final EditText ip1 = inflate.findViewById(R.id.ip1);
                final EditText ip2 = inflate.findViewById(R.id.ip2);
                final EditText ip3 = inflate.findViewById(R.id.ip3);
                final EditText ip4 = inflate.findViewById(R.id.ip4);
                final EditText dk = inflate.findViewById(R.id.dk);
                Button qd = inflate.findViewById(R.id.qd);
                ip1.setText(sp.getString("ip1", null));
                ip2.setText(sp.getString("ip2", null));
                ip3.setText(sp.getString("ip3", null));
                ip4.setText(sp.getString("ip4", null));
                dk.setText(sp.getString("dk", null));
                AlertDialog.Builder builder = new AlertDialog.Builder(SplashAt.this);
                builder.setView(inflate).create();
                final AlertDialog show = builder.show();

                qd.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String IP1 = ip1.getText().toString();
                        String IP2 = ip2.getText().toString();
                        String IP3 = ip3.getText().toString();
                        String IP4 = ip4.getText().toString();
                        String DK = dk.getText().toString();
                        if (IP1.length() == 0 | IP2.length() == 0 | IP3.length() == 0 | IP4.length() == 0 | DK.length() == 0) {
                            Toast.makeText(SplashAt.this, "不能为空！", Toast.LENGTH_SHORT).show();
                        } else if (Integer.valueOf(IP1) < 0 | Integer.valueOf(IP1) > 255 |
                                Integer.valueOf(IP2) < 0 | Integer.valueOf(IP2) > 255 |
                                Integer.valueOf(IP3) < 0 | Integer.valueOf(IP3) > 255 |
                                Integer.valueOf(IP4) < 0 | Integer.valueOf(IP4) > 255) {
                            Toast.makeText(SplashAt.this, "输入有误！", Toast.LENGTH_SHORT).show();
                        } else {
                            SharedPreferences.Editor edit = sp.edit();
                            edit.putString("ip1", IP1);
                            edit.putString("ip2", IP2);
                            edit.putString("ip3", IP3);
                            edit.putString("ip4", IP4);
                            edit.putString("dk", DK);
                            edit.apply();
                            Toast.makeText(SplashAt.this, "保存成功！", Toast.LENGTH_SHORT).show();
                            show.dismiss();
                        }
                    }
                });

                break;

        }
    }

    class spAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return imageViews.size();
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
            return view == object;
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            container.addView(imageViews.get(position));
            return imageViews.get(position);
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeView(imageViews.get(position));
        }
    }
}